package com.greate.community.service;

import com.alibaba.fastjson.JSONObject;
import com.greate.community.entity.User;
import com.greate.community.util.CosStsClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 类名称：CosService
 * 类描述：对象存储服务类
 * 创建人：CSUpipixia
 * 创建时间：2022/2/3 19:21
 *
 * @version v1.0
 */
@Slf4j
@Service
public class CosService {

    @Autowired
    private CosStsClientUtil cosStsClientUtil;

    @Autowired
    private UserService userService;

    public JSONObject getCredential(int userId, String type, List<String> fileNames) {
        List<String> filePaths = new ArrayList<String>();
        log.info("fileNames: {}", fileNames.toString());
        fileNames.forEach(name -> {
            filePaths.add(userId + "/" + name);
        });
        log.info("filePaths: {}", filePaths.toString());
        return cosStsClientUtil.getCredential(type, filePaths.toArray(new String[filePaths.size()]));
    }

    public JSONObject getAdminCredential(String type, List<String> filePaths) {
        return cosStsClientUtil.getCredential(type, filePaths.toArray(new String[filePaths.size()]));
    }
}
